package com.jyw.web.controller.system;

import java.util.List;
import java.util.Set;

import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.core.domain.model.WechatLoginBody;
import com.jyw.system.domain.SysUserDeptRole;
import com.jyw.system.domain.vo.UserInfoVo;
import com.jyw.system.service.ISysUserDeptRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.jyw.common.constant.Constants;
import com.jyw.common.core.domain.AjaxResult;
import com.jyw.common.core.domain.entity.SysMenu;
import com.jyw.common.core.domain.entity.SysUser;
import com.jyw.common.core.domain.model.LoginBody;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.framework.web.service.SysLoginService;
import com.jyw.framework.web.service.SysPermissionService;
import com.jyw.system.service.ISysMenuService;

import javax.annotation.Resource;

/**
 * 登录验证
 * 
 * @author system
 */
@RestController
@Api(tags = "登录验证")
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Resource
    private ISysUserDeptRoleService deptRoleService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public CommonResult<String> login(@RequestBody LoginBody loginBody)
    {
//        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
//        ajax.put(Constants.TOKEN, token);
        return CommonResult.success(null,token);
    }


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/wechat/login")
    @ApiOperation("小程序登录")
    public CommonResult<String> wechatLogin(@RequestBody WechatLoginBody loginBody)
    {
        // 生成令牌
        String token = loginService.wechatPhoneLogin(loginBody);
//        ajax.put(Constants.TOKEN, token);
        return CommonResult.success(null,token);
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
